/*
 * StatusListCellRend.java
 *
 * Created on April 17, 2007, 6:46 PM
 */

package org.voidness.twittava.gui;

import java.awt.Color;
import java.awt.Image;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.voidness.twittava.domain.Status;
import org.voidness.twittava.remoteimages.ImageRetriever;
import org.voidness.twittava.remoteimages.RemoteImageListener;

/**
 *
 * @author  void
 */
public class StatusPanel extends javax.swing.JPanel implements RemoteImageListener {
    
    /** Creates new form StatusListCellRend */
    public StatusPanel(Status status, int index, int totalStatuses) {
        initComponents();
        setupData(status, index, totalStatuses);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        specialPanel1 = new org.voidness.twittava.gui.SpecialPanel();
        text = new javax.swing.JTextArea();
        dateLabel = new javax.swing.JLabel();
        imagePanel = new org.voidness.twittava.gui.ImagePanel();
        nickLabel = new javax.swing.JLabel();

        specialPanel1.setOpaque(false);
        text.setColumns(20);
        text.setEditable(false);
        text.setFont(new java.awt.Font("Dialog", 0, 10));
        text.setLineWrap(true);
        text.setRows(5);
        text.setText("This is just some sample text. This is just some sample text. This is just some sample text. This is just some sample text. This is just some sample text. ");
        text.setWrapStyleWord(true);
        text.setBorder(null);
        text.setOpaque(false);

        dateLabel.setFont(new java.awt.Font("Dialog", 1, 10));
        dateLabel.setHorizontalAlignment(javax.swing.SwingConstants.TRAILING);
        dateLabel.setText("5 minutes ago");

        imagePanel.setOpaque(false);

        nickLabel.setFont(new java.awt.Font("Dialog", 3, 14));
        nickLabel.setText("jLabel1");

        javax.swing.GroupLayout specialPanel1Layout = new javax.swing.GroupLayout(specialPanel1);
        specialPanel1.setLayout(specialPanel1Layout);
        specialPanel1Layout.setHorizontalGroup(
            specialPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, specialPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(specialPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(specialPanel1Layout.createSequentialGroup()
                        .addComponent(nickLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dateLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))
                    .addGroup(specialPanel1Layout.createSequentialGroup()
                        .addComponent(imagePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(text, javax.swing.GroupLayout.DEFAULT_SIZE, 294, Short.MAX_VALUE)))
                .addContainerGap())
        );
        specialPanel1Layout.setVerticalGroup(
            specialPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(specialPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(specialPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nickLabel)
                    .addComponent(dateLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(specialPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(text, javax.swing.GroupLayout.PREFERRED_SIZE, 41, Short.MAX_VALUE)
                    .addComponent(imagePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(specialPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(specialPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    private String getHumanReadableDate(Date date) {
        Calendar now = new GregorianCalendar();
        now.setTime(new Date());
        Calendar other = new GregorianCalendar();
        other.setTime(date);
        int diffInSeconds = (int) ((now.getTimeInMillis() - other.getTimeInMillis()) / 1000);
        int diffInMinutes = diffInSeconds/60;
        int diffInHours = diffInMinutes/60;
        int diffInDays = diffInHours/24;
        
        if (diffInSeconds < 60) {
            return diffInSeconds + " second" + (diffInSeconds > 1 ? "s" : "") + " ago";
        } else if (diffInMinutes < 60) {
            return diffInMinutes + " minute" + (diffInMinutes > 1 ? "s" : "") + " ago";
        } else if (diffInHours < 24) {
            return diffInHours + " hour" + (diffInHours > 1 ? "s" : "") + " ago";
        } else if (diffInDays < 3) {
            return diffInDays + " day" + (diffInDays > 1 ? "s" : "") + " ago";
        }
        
        SimpleDateFormat format = new SimpleDateFormat("EEE, MMM d, yyyy");
        return "on " + format.format(date);
    }
    
    public void setupData(Status status, int index, int totalStatuses) {
        nickLabel.setText(status.getUser().getName());
        dateLabel.setText("posted " + getHumanReadableDate(status.getCreationDate()));
        text.setText(status.getText());
        
        setForeground(Color.black);
        Color background = getBackground();
        int alpha = index * 255 / totalStatuses;
        specialPanel1.setBack(new Color(211, 233, 255, 255-alpha));
        
        // Start getting the image
        if (imagePanel.getImage() == null) {
            ImageRetriever.getInstance().getImage(status.getUser().getProfileImageUrl(), this);
        }
    }
    
    public void onImageReceived(Image image) {
        imagePanel.setImage(image);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel dateLabel;
    private org.voidness.twittava.gui.ImagePanel imagePanel;
    private javax.swing.JLabel nickLabel;
    private org.voidness.twittava.gui.SpecialPanel specialPanel1;
    private javax.swing.JTextArea text;
    // End of variables declaration//GEN-END:variables

    public String toString() {
        return "StatusPanel";
    }
    
}
